package com.usian.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.admin.AdChannelControllerApi;
import com.usian.admin.service.AdChannelService;
import com.usian.common.aliyun.ScanTest;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.utils.common.BCrypt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/channel")
@Api(value = "频道管理",tags = "channel",description = "频道管理接口文档")
public class AdChannelController implements AdChannelControllerApi {

    @Autowired
    private AdChannelService adChannelService;

    @PostMapping("/list")
    @Override
    @ApiOperation(value = "频道列表查询接口",notes = "频道列表根据名称分页模糊查询")
    @ApiImplicitParam(name = "dto",type ="ChannelDto",value = "分页查询参数，携带page、size")
    public ResponseResult findByName(@RequestBody ChannelDto dto) {
        return adChannelService.findByNameAndPage(dto);
    }

    @PostMapping("/save")
    @Override
    public ResponseResult save(@RequestBody AdChannel adChannel) {
        return adChannelService.saveChannel(adChannel);
    }

    @PostMapping("/update")
    @Override
    public ResponseResult update(@RequestBody AdChannel adChannel) {
        return adChannelService.updateChanel(adChannel);
    }

    @GetMapping("/del/{id}")
    @Override
    public ResponseResult deleteById(@PathVariable Integer id) {
        return adChannelService.deleteById(id);
    }

    /**
     * 查询频道列表
     *
     * @return
     */
    @Override
    @GetMapping("/channels")
    public ResponseResult channels() {

        List<AdChannel> adChannelList = adChannelService.list(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getStatus, 1));
        return ResponseResult.okResult(adChannelList);
    }


    @Autowired
    FastDFSClient fastDFSClient;
    @Autowired
    ScanTest scanTest;
    @GetMapping("/test")
    public String test(){

        try {
            String url ="http://192.168.58.138/group1/M00/00/01/wKg6imTuqCuAAF3BAAASp690Yak599.jpg";
            String groupUrl =url.replace("http://192.168.58.138/","");
            String group =groupUrl.substring(0,groupUrl.indexOf("/"));
            String imgPath = groupUrl.replace(group,"").substring(1);
            byte[] bytes = fastDFSClient.download(group, imgPath);

           /* Map<String, Object> imageMap = scanTest.scanImage(bytes);
            System.out.println("imageMap"+imageMap);


            String content ="冰毒";
            Map<String, Object> testMap = scanTest.scanText(content);
            System.out.println("textMap"+imageMap);*/

            String a=  "{" +
                    "                    \"showapi_res_error\": \"\"," +
                    "                    \"showapi_res_id\": \"64f085de0de376f5612a7cfb\"," +
                    "                    \"showapi_res_code\": 0," +
                    "                    \"showapi_fee_num\": 1," +
                    "                    \"showapi_res_body\": {\"sug\":\"pass\",\"ret_code\":0,\"confidence_level\":0,\"results\":[]}" +
                    "                }";

            Map<String,Object> map = JSON.parseObject(a, Map.class);
            System.out.println(map);
            JSONObject showapi_res_body =(JSONObject) map.get("showapi_res_body");
            Object sug = showapi_res_body.get("sug");
            System.out.println(sug);


          /*String b="{showapi_res_error=, showapi_fee_num=1, showapi_res_id=64f08c1d0de376f561d607fa, showapi_res_body={\"sug\":\"pass\",\"confidence_level\":0,\"ret_code\":0,\"results\":[]}, showapi_res_code=0}" ;
          Map<String,Object> map = JSON.parseObject(b, Map.class);
            String showapi_res_body = (String)map.get("showapi_res_body");
            Map<String,Object> resultMap = JSON.parseObject(showapi_res_body, Map.class);
            String sug = (String)resultMap.get("sug");
            System.out.println(sug);*/





        } catch (IOException e) {
            e.printStackTrace();
        }

        return "abc";
    }

}
